package com.marvels.approve.mqueues.service.impl;

import com.marvels.approve.mqueues.dto.LoanApplyQueueDTO;
import com.marvels.approve.mqueues.service.ApproveILoanApplyMqReciever;
import com.marvels.approve.mqueues.service.ApproveILoanApplyQueueService;
import com.marvels.common.utils.MarvelsLogUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Service;

/**从MQ中获取进件信息
 * @author 何杰
 * 2018年4月21日
 */ 
@Service
@EnableScheduling
public class ApproveLoanApplyMqReciever implements ApproveILoanApplyMqReciever {
	@Autowired
	private ApproveILoanApplyQueueService loanApplyQueueService;
	
	public void process(String loanId) throws Exception {
		LoanApplyQueueDTO loanApplyQueue = loanApplyQueueService.findLoanApplyQueueLoanId(loanId);
		loanApplyQueue.setRetryCount(1);
		loanApplyQueueService.processItem(loanApplyQueue);
	}

	public void onMessage(String loanId){
        MarvelsLogUtil.getInstance().info("ApproveLoanApplyMqReciever.onMessage loanId:" + loanId);
		try {
			if(StringUtils.isNotBlank(loanId)) {
				loanId = loanId.replaceAll("\"", "");
				LoanApplyQueueDTO loanApplyQueue = loanApplyQueueService.processQueue(loanId);
				loanApplyQueueService.processItem(loanApplyQueue);
			}
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("取件失败"+loanId,e);
		}
	}

}